package com.qiaoba.module.log.controller;

import com.github.pagehelper.PageHelper;
import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.entity.param.TimeParam;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.log.entity.SysLog;
import com.qiaoba.module.log.entity.param.SysLogParam;
import com.qiaoba.module.log.service.SysLogService;
import com.qiaoba.poi.excel.util.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志 web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@RestController
@RequestMapping("/sys/log")
@RequiredArgsConstructor
public class SysLogController {

    private final SysLogService sysLogService;

    @GetMapping("/list")
    public PageResult<SysLog> list(SysLogParam param, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysLog> list = sysLogService.selectList(param);
        return PageResult.build(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogParam param) {
        List<SysLog> list = sysLogService.selectList(param);
        ExcelUtil.exportExcel(list, SysLog.class, "操作日志", response);
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable List<String> ids) {
        return Result.toAjax(sysLogService.delete(ids));
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Result clean(TimeParam param) {
        sysLogService.delLog(param);
        return Result.success();
    }
}
